package com.yellowpage.sample.action;
  
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.sample.data.HtmlTemplate;
import com.yellowpage.sample.service.HtmlTemplateService;

public class HtmlTemplateAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;
	
	private List<HtmlTemplate> htmlTemplateList = new ArrayList<HtmlTemplate>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private HtmlTemplate htmlTemplate;
	private HtmlTemplateService htmlTemplateService;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	
		private String query_htmlTemplateId;
		private String query_resourceId;
		private String query_createTime;
		private String query_createUser;
		private String query_lastUpdateTime;
		private String query_lastUpdateUser;
		private String query_name;
		private String query_type;
		private String query_status;
		
	public void prepare() {
		super.prepare();
	
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.htmlTemplate = htmlTemplateService.getHtmlTemplate(Long.parseLong(selectedId));
		}
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
			    queryMap.put("htmlTemplateId", query_htmlTemplateId);
	    	    queryMap.put("resourceId", query_resourceId);
	    	    queryMap.put("createTime", query_createTime);
	    	    queryMap.put("createUser", query_createUser);
	    	    queryMap.put("lastUpdateTime", query_lastUpdateTime);
	    	    queryMap.put("lastUpdateUser", query_lastUpdateUser);
	    	    queryMap.put("name", query_name);
	    	    queryMap.put("type", query_type);
	    	    queryMap.put("status", query_status);
	    	    	
		resultList = htmlTemplateService.queryHtmlTemplateByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {
	    
		htmlTemplateService.saveHtmlTemplate(htmlTemplate);		
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
	    	    	    	    	    	    	    	    	    	    	    	    	    	    	    if (htmlTemplate.getName().length() == 0) {
			this.addActionError("** Name 不能为空 !");
		}
			    	    	    if (htmlTemplate.getType().length() == 0) {
			this.addActionError("** Type 不能为空 !");
		}
			    	    	    if (htmlTemplate.getStatus().length() == 0) {
			this.addActionError("** Status 不能为空 !");
		}
			    	    
		this.setValidatorResult("/sample/HtmlTemplateAdd.jsp");		
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    htmlTemplate = htmlTemplateService.getHtmlTemplate(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    htmlTemplateService.updateHtmlTemplate(htmlTemplate);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
					    			    			    			    			    			    			    if (htmlTemplate.getName().length() == 0) {
			this.addActionError("** Name 不能为空 !");
		}
			    			    if (htmlTemplate.getType().length() == 0) {
			this.addActionError("** Type 不能为空 !");
		}
			    			    if (htmlTemplate.getStatus().length() == 0) {
			this.addActionError("** Status 不能为空 !");
		}
			    		this.setValidatorResult("/sample/HtmlTemplateUpdate.jsp");	
	}
	
	public String delete() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		this.htmlTemplateService.delete(ids);
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setHtmlTemplate(HtmlTemplate htmlTemplate) {
		this.htmlTemplate = htmlTemplate;
	}
	
	public HtmlTemplate getHtmlTemplate() {
		return htmlTemplate;
	}
	 
	 
	 public void setHtmlTemplateService(HtmlTemplateService htmlTemplateService) {
		this.htmlTemplateService = htmlTemplateService;
	}
	
	public List<HtmlTemplate> getHtmlTemplateList() {
		return htmlTemplateList;
	}

	public void setHtmlTemplateList(List<HtmlTemplate> htmlTemplateList) {
		this.htmlTemplateList = htmlTemplateList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_htmlTemplateId() {
        return this.query_htmlTemplateId;
    }
    
    public void setQuery_htmlTemplateId(String htmlTemplateId){
        this.query_htmlTemplateId = htmlTemplateId;
    }    

        public String getQuery_resourceId() {
        return this.query_resourceId;
    }
    
    public void setQuery_resourceId(String resourceId){
        this.query_resourceId = resourceId;
    }    

        public String getQuery_createTime() {
        return this.query_createTime;
    }
    
    public void setQuery_createTime(String createTime){
        this.query_createTime = createTime;
    }    

        public String getQuery_createUser() {
        return this.query_createUser;
    }
    
    public void setQuery_createUser(String createUser){
        this.query_createUser = createUser;
    }    

        public String getQuery_lastUpdateTime() {
        return this.query_lastUpdateTime;
    }
    
    public void setQuery_lastUpdateTime(String lastUpdateTime){
        this.query_lastUpdateTime = lastUpdateTime;
    }    

        public String getQuery_lastUpdateUser() {
        return this.query_lastUpdateUser;
    }
    
    public void setQuery_lastUpdateUser(String lastUpdateUser){
        this.query_lastUpdateUser = lastUpdateUser;
    }    

        public String getQuery_name() {
        return this.query_name;
    }
    
    public void setQuery_name(String name){
        this.query_name = name;
    }    

        public String getQuery_type() {
        return this.query_type;
    }
    
    public void setQuery_type(String type){
        this.query_type = type;
    }    

        public String getQuery_status() {
        return this.query_status;
    }
    
    public void setQuery_status(String status){
        this.query_status = status;
    }    

    
	
}
